package com.sky.wsp.jar.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.yaml.snakeyaml.Yaml;

public class YamlUtils {
  private static final String REGEX_BRACKET = "[\\[\\]]";
  private static final String REGEX_BACKSLASH = "\\\\";
  private static final String REGEX_INSIDE = "''";
  private static final String REGEX_HEAD = "'\"";
  private static final String REGEX_TAIL = "\"'";
  private static final String SINGLE_QUOTED = "'";
  private static final String DOUBLE_QUOTED = "\"";
  private static final Map<String, String> cacheYamlMap = new HashMap();

  public YamlUtils() {
  }

  public static String formatYamlStr(String str) {
    Pattern p = Pattern.compile("[\\[\\]]");
    Matcher m = p.matcher(str);
    str = m.replaceAll("").trim();
    p = Pattern.compile("\\\\");
    m = p.matcher(str);
    str = m.replaceAll("");
    p = Pattern.compile("''");
    m = p.matcher(str);
    str = m.replaceAll("'");
    p = Pattern.compile("'\"");
    m = p.matcher(str);
    str = m.replaceAll("\"");
    p = Pattern.compile("\"'");
    m = p.matcher(str);
    return m.replaceAll("\"");
  }

  public static Object transListToLinkMap(List<String> yamlKey) {
    Map<String, Object> linkMap = new LinkedHashMap();
    Iterator var2 = yamlKey.iterator();

    while(var2.hasNext()) {
      String val = (String)var2.next();
      Map<String, Object> map = (Map)transStringToLinkMap(val);
      addYamlMap(linkMap, map);
    }

    return linkMap;
  }

  public static void addAndRemoveYaml(Map<String, Object> originMap, Map<String, Object> removeMap, Map<String, Object> addMap) {
    removeYamlMap(originMap, removeMap);
    addYamlMap(originMap, addMap);
  }

  public static boolean removeYamlMap(Map<String, Object> originMap, Map<String, Object> removeMap) {
    Iterator var2 = removeMap.entrySet().iterator();

    while(var2.hasNext()) {
      Map.Entry<String, Object> val = (Map.Entry)var2.next();
      Iterator var4 = originMap.entrySet().iterator();

      while(var4.hasNext()) {
        Map.Entry<String, Object> obj = (Map.Entry)var4.next();
        if (((String)val.getKey()).equals(obj.getKey())) {
          try {
            Map<String, Object> objVal = (Map)obj.getValue();
            Map<String, Object> addVal = (Map)val.getValue();
            if (removeYamlMap(objVal, addVal)) {
              originMap.remove(val.getKey());
              break;
            }
          } catch (Exception var8) {
            originMap.remove(val.getKey());
            break;
          }
        }
      }
    }

    if (originMap != null && !originMap.isEmpty()) {
      return false;
    } else {
      return true;
    }
  }

  public static void addYamlMap(Map<String, Object> originMap, Map<String, Object> addMap) {
    Iterator var2 = addMap.entrySet().iterator();

    while(var2.hasNext()) {
      Map.Entry<String, Object> val = (Map.Entry)var2.next();
      boolean common = false;
      Iterator var5 = originMap.entrySet().iterator();

      while(var5.hasNext()) {
        Map.Entry<String, Object> obj = (Map.Entry)var5.next();
        if (((String)val.getKey()).equals(obj.getKey())) {
          common = true;

          try {
            Map<String, Object> objVal = (Map)obj.getValue();
            Map<String, Object> addVal = (Map)val.getValue();
            addYamlMap(objVal, addVal);
          } catch (Exception var9) {
          }
          break;
        }
      }

      if (!common) {
        originMap.put(val.getKey(), val.getValue());
      }
    }

  }

  private static Object transStringToLinkMap(String val) {
    int i = val.indexOf(".");
    if (i < 0) {
      Map<String, Object> map = new LinkedHashMap();
      map.put(val, "");
      return map;
    } else {
      String key = val.substring(0, i);
      String value = val.substring(i + 1);
      Map<String, Object> linkedHashMap = (Map)transStringToLinkMap(value);
      Map<String, Object> map = new LinkedHashMap();
      map.put(key, linkedHashMap);
      return map;
    }
  }

  public static List<String> compareAndGetDiffAttr(String oldContent, String newContent, ConstantEnum type) {
    Yaml yaml = new Yaml();
    Iterable<Object> oldYaml = yaml.loadAll(oldContent);
    List<String> oldKey = analyzeYamlToList(oldYaml);
    Iterable<Object> newYaml = yaml.loadAll(newContent);
    List<String> recent = analyzeYamlToList(newYaml);
    ArrayList compareList;
    if (ConstantEnum.ADD.equals(type)) {
      compareList = new ArrayList(recent);
      compareList.removeAll(oldKey);
      return compareList;
    } else {
      compareList = new ArrayList(oldKey);
      compareList.removeAll(recent);
      return compareList;
    }
  }

  public static List<String> analyzeYamlToList(Iterable<Object> newYaml) {
    Iterator it = newYaml.iterator();

    while(it.hasNext()) {
      Map<String, Object> object = (Map)it.next();
      parseYamlToMap(object, (String)null);
    }

    List<String> keyList = new ArrayList();
    cacheYamlMap.forEach((key, val) -> {
      keyList.add(key);
    });
    cacheYamlMap.clear();
    return keyList;
  }

  public static void parseYamlToMap(Map<String, Object> item, String key) {
    item.forEach((k, v) -> {
      if (Objects.isNull(v)) {
        if (key == null) {
          cacheYamlMap.put(k, "");
        } else {
          cacheYamlMap.put(key.concat(".").concat(k), "");
        }
      } else if (v instanceof LinkedHashMap) {
        if (key == null) {
          parseYamlToMap((Map)v, k);
        } else {
          parseYamlToMap((Map)v, key.concat(".").concat(k));
        }
      } else if (key == null) {
        cacheYamlMap.put(k, v.toString());
      } else {
        cacheYamlMap.put(key.concat(".").concat(k), v.toString());
      }

    });
  }
}